package callnumber.gtdev5.com.photoVault.fragment;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gtdev5.geetolsdk.mylibrary.callback.BaseCallback;
import com.gtdev5.geetolsdk.mylibrary.http.HttpUtils;
import com.gtdev5.geetolsdk.mylibrary.util.ToastUtils;
import com.gtdev5.geetolsdk.mylibrary.util.Utils;

import org.litepal.crud.DataSupport;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import callnumber.gtdev5.com.photoVault.R;
import callnumber.gtdev5.com.photoVault.activity.ChangePasswordActivity;
import callnumber.gtdev5.com.photoVault.activity.SuggestListActivity;
import callnumber.gtdev5.com.photoVault.base.BaseFragment;
import callnumber.gtdev5.com.photoVault.bean.Contract;
import callnumber.gtdev5.com.photoVault.bean.GetNewBean;
import callnumber.gtdev5.com.photoVault.bean.UpdateBean;
import callnumber.gtdev5.com.photoVault.utils.FileSizeUtil;
import callnumber.gtdev5.com.photoVault.widget.DialogDownLoadAPK;
import okhttp3.Request;
import okhttp3.Response;

import static callnumber.gtdev5.com.photoVault.utils.FileSizeUtil.SIZETYPE_GB;

/**
 * Created by cheng
 * PackageName FakeCall_4
 * 2018/8/15 15:49
 */

public class AboutFragment extends BaseFragment {
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.app_name)
    TextView appName;
    @BindView(R.id.already_Used)
    TextView alreadyUsed;
    @BindView(R.id.version_code)
    TextView versionCode;
    @BindView(R.id.rv_icon)
    RelativeLayout rvIcon;
    @BindView(R.id.lv_feedback)
    RelativeLayout lvFeedback;
    @BindView(R.id.lv_checkUpdate)
    RelativeLayout lvCheckUpdate;
    @BindView(R.id.txt_conract)
    TextView txtConract;
    Unbinder unbinder;
    private UpdateBean bean;
    Contract contractBean;
    @BindView(R.id.text_Total)
    TextView textTotal;
@BindView(R.id.txt_Password)
RelativeLayout txtPassword;
    @Override
    protected int getLayouId() {
        return R.layout.fragment_about;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

    }

    @Override
    protected void initDatas() {
        versionCode.setText("v" + getVersionCode());
        bean = DataSupport.findFirst(UpdateBean.class);
        contractBean = DataSupport.findFirst(Contract.class);
        //联系电话
        if (contractBean != null) {
            txtConract.setText("联系方式" + ":" + contractBean.getTxt() + "-" + contractBean.getNum());
        }
        if (!vip.isIsout()) {
            textTotal.setText("不限量");
        }else {
            textTotal.setText("100M");
        }
    }

    @Override
    protected void initAction() {
        txtPassword.setOnClickListener(v -> {
            Intent intent=new Intent(getActivity(),ChangePasswordActivity.class);
            startActivity(intent);
        });

        //意见反馈
        lvFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SuggestListActivity.class));
            }
        });
        //检查更新
        lvCheckUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });
        String filePath = "/sdcard/encryption";
        File file = new File(filePath);
//判断文件夹是否存在，如果不存在就创建，否则不创建
        if (file.exists()) {
            //通过file的mkdirs()方法创建目录中包含却不存在的文件夹
            double s = FileSizeUtil.getFileOrFilesSize(filePath, 4);
            double two = 100;
            String re = FileSizeUtil.getDouble(s, two);
            if (re.equals("1")) {

            }
            String d = FileSizeUtil.getAutoFileOrFilesSize(filePath);

            alreadyUsed.setText(d + "");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        String filePath = "/sdcard/encryption";
        File file = new File(filePath);
        if (file.exists()) {
            String d = FileSizeUtil.getAutoFileOrFilesSize(filePath);
            alreadyUsed.setText(d + "");
        }
    }

    //获取版本号
    private String getVersionCode() {
        String localVersion = null;
        try {
            PackageInfo packageInfo = getActivity().getApplicationContext().getPackageManager().getPackageInfo(getActivity().getPackageName(), 0);
            localVersion = packageInfo.versionName;
            // LogUtil.d("TAG", "本软件的版本号。。" + localVersion);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }

    private void update() {
        if (Utils.isNetworkAvailable(getActivity())) {
            HttpUtils.getInstance().postNews(new BaseCallback<GetNewBean>() {
                @Override
                public void onRequestBefore() {

                }

                @Override
                public void onFailure(Request request, Exception e) {

                }

                @Override
                public void onSuccess(Response response, GetNewBean o) {
                    if (response.isSuccessful()) {
                        if (o.isHasnew()) {
                            DialogDownLoadAPK loadAPK = new DialogDownLoadAPK(getActivity(), o.getVername() + "来啦");
                            loadAPK.show();
                            loadAPK.setOnClickListener(clicked -> {
                                if (clicked) {
                                    openActionView(o.getDownurl());
                                }
                            });
                        } else {
                            ToastUtils.showShortToast("已经是最新版本");
                        }
                    }
                }

                @Override
                public void onError(Response response, int errorCode, Exception e) {

                }
            });
        } else {
            ToastUtils.showLongToast("请检查网络链接");
        }//
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}

